package com.damuzee.system.system.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.mybatis.sql.page.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.damuzee.system.system.domain.PermModule;
import com.damuzee.system.system.domain.PermRole;
import com.damuzee.system.system.service.PermModuleService;
import com.damuzee.system.system.service.PermRoleService;

@Controller
public class RoleAct {

	@RequestMapping(value="/role/v_list")
	public String list(Integer pageNo,HttpServletRequest request,ModelMap map ){
		PermRole role = new PermRole();
		
		Page<PermRole> page = new Page<PermRole>();
		page.setCurrentPage(pageNo);
		
		page = roleService.getPermRoleList(page, role);
		
		map.addAttribute("page",page.getPage() );
		return "/role/list";
	}
	
	@RequestMapping(value="/role/v_tree.do")
	public String tree(String root, HttpServletRequest request,
			HttpServletResponse response, ModelMap model){
		
		boolean isRoot;
		// jquery treeview的根请求为root=source
		if (StringUtils.isBlank(root) || "source".equals(root)) {
			isRoot = true;
		} else {
			isRoot = false;
		}
		model.addAttribute("isRoot", isRoot);
		List<PermModule> list = null;
		if (isRoot) {
			list = moduleService.getChildList(new Long(1));
		} else {
			Integer rootId = Integer.valueOf(root);
			list = moduleService.getChildList(new Long(rootId));
		}
		model.addAttribute("list", list);
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=UTF-8");
		return "/module/tree";
	}
	
	
	@RequestMapping(value="/role/v_add.do")
	public String add(HttpServletRequest request,ModelMap map){
		
		return "/role/add" ;
	}
	@Resource
	private PermRoleService roleService; 
	
	@Resource
	private PermModuleService moduleService ;
	
}
